package org.academic.texteditor.gui.component.listener;

import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;

import org.academic.texteditor.gui.component.PersonalizedTextPane;

public class DragCopyTextListener implements DragGestureListener, DragSourceListener {

	private final PersonalizedTextPane textPane;

	private final DragSource ds = DragSource.getDefaultDragSource();
	
	public DragCopyTextListener(PersonalizedTextPane textPane) {
		this.textPane = textPane;
		
		int action = DnDConstants.ACTION_COPY_OR_MOVE;
		ds.createDefaultDragGestureRecognizer(textPane, action, this);
	}
	
	@Override
	public void dragGestureRecognized(DragGestureEvent e) {
		try {
			Transferable t = new ParagraphTransferable(textPane.getParagraph());

			e.startDrag(DragSource.DefaultMoveNoDrop, t, this);
		} catch (Exception e2) {
		}
	}

	@Override
	public void dragDropEnd(DragSourceDropEvent e) {
		DragSourceContext ctx = e.getDragSourceContext();

	    int action = e.getDropAction();
	    if ((action & DnDConstants.ACTION_MOVE) != 0)
	      ctx.setCursor(DragSource.DefaultMoveDrop);
	    else
	      ctx.setCursor(DragSource.DefaultMoveNoDrop);
	}

	@Override
	public void dragEnter(DragSourceDragEvent e) {
		DragSourceContext ctx = e.getDragSourceContext();

	    int action = e.getDropAction();
	    if ((action & DnDConstants.ACTION_MOVE) != 0)
	      ctx.setCursor(DragSource.DefaultMoveDrop);
	    else
	      ctx.setCursor(DragSource.DefaultMoveNoDrop);
	}

	@Override
	public void dragExit(DragSourceEvent e) {
		DragSourceContext ctx = e.getDragSourceContext();
		ctx.setCursor(DragSource.DefaultMoveNoDrop);
	}

	@Override
	public void dragOver(DragSourceDragEvent dsde) {

	}

	@Override
	public void dropActionChanged(DragSourceDragEvent dsde) {
		
	}

}
